package com.ma.panel;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.eltima.components.ui.DatePicker;
import com.ma.listener.TradePanelListener;

public class TradePanel extends JDialog {

	private TradePanel self = this;

	private JLabel labelQuantity = new JLabel("资金量(元)");
	private JLabel labelBuyRate = new JLabel("买入比例(%)");
	private JLabel labelSellRate = new JLabel("卖出比例(%)");
	private JLabel labelBuyShort = new JLabel("买入.短线天数");
	private JLabel labelBuyLong = new JLabel("买入.长线天数");
	private JLabel labelSellShort = new JLabel("卖出.短线天数");
	private JLabel labelSellLong = new JLabel("卖出.长线天数");
	private JLabel labelDateStart = new JLabel("起始日期");
	private JLabel labelDateEnd = new JLabel("终止日期");
	private JLabel labelCommision = new JLabel("佣金(买卖)");
	private JLabel labelStampDuty = new JLabel("印花税(卖)");

	private JTextField fieldQuantity = new JTextField();
	private JComboBox fieldBuyRate = new JComboBox();
	private JComboBox fieldSellRate = new JComboBox();
	private JComboBox fieldBuyShort = new JComboBox();
	private JComboBox fieldBuyLong = new JComboBox();
	private JComboBox fieldSellShort = new JComboBox();
	private JComboBox fieldSellLong = new JComboBox();
	private JComboBox fieldCommision = new JComboBox();
	private JComboBox fieldStampDuty = new JComboBox();

	private DatePicker datepickStart;
	private DatePicker datepickEnd;

	private static final String DefaultFormat = "yyyy-MM-dd";
	private Font font = new Font("Times New Roman", Font.BOLD, 14);
	private Dimension dimension = new Dimension(150, 24);

	private int[] anIntArray = { 1, 5, 10, 20, 30, 60, 120 }; // 均线天数列表

	private JButton subBtn = new JButton("确定");
	private JButton cnlBtn = new JButton("退出");

	private TradePanelListener lis = new TradePanelListener();

	public TradePanel() {
		initProperty();
		initPosition();
		addComponents();
		initEventListener();
	}

	/**
	 * 设置面板属性
	 */
	private void initProperty() {
		try {

			this.setLayout(null);
			this.setAlwaysOnTop(true);
			this.setModal(true);

			Date dateStart = new SimpleDateFormat("yyyy-MM-dd").parse("2010-02-12");
			
			Date today = new Date() ;
			Calendar ca = Calendar.getInstance() ;
			ca.setTime(today) ;
			ca.add(Calendar.DAY_OF_MONTH, -1);
			Date dateEnd = ca.getTime();

			datepickStart = new DatePicker(dateStart, DefaultFormat, font, dimension);
			datepickEnd = new DatePicker(dateEnd, DefaultFormat, font, dimension);

			datepickStart.setLocale(Locale.CHINA);// 设置国家
			datepickStart.setBorder(null);
			// datepickStart.setTimePanleVisible(true);// 设置时钟面板可见

			datepickEnd.setLocale(Locale.CHINA);// 设置国家
			datepickEnd.setBorder(null);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	/**
	 * 初始化监听事件
	 */
	private void initEventListener() {
		cnlBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1) {
					self.dispose();
				}
			}
		});

		lis.setPanel(self);
		subBtn.addMouseListener(lis);
	}

	/**
	 * 添加组件
	 */
	private void addComponents() {
		add(labelQuantity);
		add(fieldQuantity);

		add(labelBuyRate);
		add(fieldBuyRate);

		add(labelSellRate);
		add(fieldSellRate);

		add(labelBuyShort);
		add(labelBuyLong);
		add(labelSellShort);
		add(labelSellLong);
		add(fieldBuyShort);
		add(fieldBuyLong);
		add(fieldSellShort);
		add(fieldSellLong);

		add(labelCommision);
		add(fieldCommision);
		add(labelStampDuty);
		add(fieldStampDuty);

		add(labelDateStart);
		add(labelDateEnd);
		add(datepickStart);
		add(datepickEnd);

		add(subBtn);
		add(cnlBtn);
	}

	private void initPosition() {
		initSelfPosition();
		initComponentsPosition();
	}

	/**
	 * 初始化各组件位置、初始值
	 */
	private void initComponentsPosition() {
		int width = 85;
		int filedWidth = 70;
		int height = 25;

		//资金量
		fieldQuantity.setText("1000000");
		
		//均线天数
		for (int d : anIntArray) {
			fieldBuyShort.addItem(d);
			fieldBuyLong.addItem(d);
			fieldSellShort.addItem(d);
			fieldSellLong.addItem(d);
		}
		fieldBuyLong.setSelectedIndex(2);
		fieldSellLong.setSelectedIndex(3);

		//买卖资金比例
		for (int i = 10; i <= 100;) {
			fieldBuyRate.addItem(i);
			fieldSellRate.addItem(i);
			i += 10;
		}
		fieldSellRate.setSelectedIndex(4);

		// 佣金
		fieldCommision.addItem("0.0005");
		fieldCommision.addItem("0.003");

		// 印花税
		fieldStampDuty.addItem("0.003");
		fieldStampDuty.addItem("0.0015");

		labelQuantity.setBounds(10, 10, width, height);
		fieldQuantity.setBounds(labelQuantity.getX() + labelQuantity.getWidth(), labelQuantity.getY(), 220, height);

		labelBuyShort.setBounds(10, labelQuantity.getY() + 30, width, height);
		fieldBuyShort.setBounds(labelBuyShort.getX() + labelBuyShort.getWidth(), labelBuyShort.getY(), filedWidth, height);

		labelBuyLong.setBounds(10, labelBuyShort.getY() + 30, width, height);
		fieldBuyLong.setBounds(labelBuyLong.getX() + labelBuyLong.getWidth(), labelBuyLong.getY(), filedWidth, height);

		labelSellShort.setBounds(10, labelBuyLong.getY() + 30, width, height);
		fieldSellShort.setBounds(labelSellShort.getX() + labelSellShort.getWidth(), labelSellShort.getY(), filedWidth, height);

		labelSellLong.setBounds(10, labelSellShort.getY() + 30, width, height);
		fieldSellLong.setBounds(labelSellLong.getX() + labelSellLong.getWidth(), labelSellLong.getY(), filedWidth, height);

		labelBuyRate.setBounds(fieldBuyShort.getX() + fieldBuyShort.getWidth() + 10, fieldBuyShort.getY(), width, height);
		fieldBuyRate.setBounds(labelBuyRate.getX() + labelBuyRate.getWidth(), labelBuyRate.getY(), filedWidth, height);

		labelSellRate.setBounds(labelBuyRate.getX(), labelBuyRate.getY() + 30, width, height);
		fieldSellRate.setBounds(labelSellRate.getX() + labelSellRate.getWidth(), labelSellRate.getY(), filedWidth, height);

		labelDateStart.setBounds(labelSellRate.getX(), labelSellRate.getY() + 30, width, height);
		datepickStart.setBounds(labelDateStart.getX() + labelDateStart.getWidth(), labelDateStart.getY(), 87, height);

		labelDateEnd.setBounds(labelDateStart.getX(), labelDateStart.getY() + 30, width, height);
		datepickEnd.setBounds(labelDateEnd.getX() + labelDateEnd.getWidth(), labelDateEnd.getY(), 87, height);

		labelCommision.setBounds(10, labelSellLong.getY() + 30, width, height);
		fieldCommision.setBounds(labelCommision.getX() + labelCommision.getWidth(), labelCommision.getY(), filedWidth, height);

		labelStampDuty.setBounds(labelCommision.getX(), labelCommision.getY() + 30, width, height);
		fieldStampDuty.setBounds(labelStampDuty.getX() + labelStampDuty.getWidth(), labelStampDuty.getY(), filedWidth, height);

		subBtn.setBounds(200, labelQuantity.getY() + 170, 60, height);
		cnlBtn.setBounds(subBtn.getX() + subBtn.getWidth() + 10, subBtn.getY(), 60, height);
	}

	/**
	 * 设定主程序窗口的位置
	 */
	private void initSelfPosition() {
		Dimension screenD = this.getToolkit().getScreenSize();
		int width = (int) screenD.getWidth();
		int height = (int) screenD.getHeight();
		setSize(370, 250);
		setTitle("模拟均线交易");
		setLocation((width - 350) / 2, (height - 250) / 2);
	}

	/**
	 * @return the fieldQuantity
	 */
	public JTextField getFieldQuantity() {
		return fieldQuantity;
	}

	/**
	 * @return the fieldBuyRate
	 */
	public JComboBox getFieldBuyRate() {
		return fieldBuyRate;
	}

	/**
	 * @return the fieldSellRate
	 */
	public JComboBox getFieldSellRate() {
		return fieldSellRate;
	}

	/**
	 * @return the fieldBuyShort
	 */
	public JComboBox getFieldBuyShort() {
		return fieldBuyShort;
	}

	/**
	 * @return the fieldBuyLong
	 */
	public JComboBox getFieldBuyLong() {
		return fieldBuyLong;
	}

	/**
	 * @return the fieldSellShort
	 */
	public JComboBox getFieldSellShort() {
		return fieldSellShort;
	}

	/**
	 * @return the fieldSellLong
	 */
	public JComboBox getFieldSellLong() {
		return fieldSellLong;
	}

	/**
	 * @return the datepickStart
	 */
	public DatePicker getDatepickStart() {
		return datepickStart;
	}

	/**
	 * @return the datepickEnd
	 */
	public DatePicker getDatepickEnd() {
		return datepickEnd;
	}

	/**
	 * @return the fieldCommision
	 */
	public JComboBox getFieldCommision() {
		return fieldCommision;
	}

	/**
	 * @return the fieldStampDuty
	 */
	public JComboBox getFieldStampDuty() {
		return fieldStampDuty;
	}

	/**
	 * @param fieldQuantity
	 *            the fieldQuantity to set
	 */
	public void setFieldQuantity(JTextField fieldQuantity) {
		this.fieldQuantity = fieldQuantity;
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				TradePanel d = new TradePanel();
				d.setVisible(true);
			}
		});
	}
}
